<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8" />
	<title>括号检查器</title>
</head>
<body>
	<div>
		<p><textarea cols="30" rows="10" id="Input"></textarea></p>
		<p><button id="Check">检查</button></p>
		<p><output></output></p>
	</div>
	<script src="../../src/Stack.js"></script>
	<script>
		var elmCheck = document.querySelector('#Check'),
				elmInput = document.querySelector('#Input'),
				elmOutput = document.querySelector('output');
		const matches = {
			'}': '{',
			']': '[',
			')': '('
		};
		elmCheck.onclick = function(){
			var strValue = elmInput.value,
					ln = strValue.length,
					stack = new Stack(ln);
			for(var i=0; i<ln; i++){
				var currVal = strValue[i];
				if( currVal == '{' ) stack.push(currVal);
				if( currVal == '(' ) stack.push(currVal);
				if( currVal == '[' ) stack.push(currVal);
				if( currVal == '}' || currVal == ']' || currVal == ')' ) {
					var stackTop = stack.peek();
					if( matches[currVal] != stackTop ){
						elmOutput.value = '不匹配';
						return;
					}
					stack.pop();
				}
			}
			if( !stack.isEmpty() ) elmOutput.value = '不匹配';
			else elmOutput.value = '检查结束';
		};
	</script>
</body>
</html>